<template>
  <!-- 风险管理列表 -->
  <div class="page-detail">
    <base-breadcrumb>
      <!-- 搜索表单 -->
      <searchData
        :search-data="searchArr"
        @search="search"
      />
      <a-card :bordered="false">
        <!-- 表格 -->
        <list-table
          ref="table"
          :columns="columns"
          :statistics-config="{ use: true, statistics, temp }"
          :request="loadData"
          :export-permission="{ select: 'lcm_006_exp_04', all: 'lcm_006_exp_04' }"
          :export-config="{ use: true, select: '风险列表选中', all: '风险列表全部', customColumns: customColumns }"
        >
          <!-- 新增风险按钮 -->
          <template
            slot="prefix"
          >
            <a-button
              v-btn-permission="'lcm_006_add_02'"
              type="primary"
              icon="plus"
              @click="$router.push({ path: 'add' })"
            >
              新增
            </a-button>
          </template>
          <!-- 序号 -->
          <span
            slot="index"
            slot-scope="text, record, index"
          >{{ index + 1 }}</span>
          <!-- 法律风险状态 -->
          <template
            slot="auditStatusStr"
            slot-scope="text, record"
          >
            <StatusDisplay
              :status-map="statusMap"
              :status-code="record.auditStatus"
            />
            <!-- <list-status :status="record.auditStatus" :text="text"></list-status> -->
          </template>
          <!-- 操作栏 -->
          <template
            slot="operation"
            slot-scope="text, record"
          >
            <MoreButton>
              <a
                v-btn-permission="'lcm_006_loo_01'"
                @click="$router.push({ path: 'detail', query: { id: record.id } })"
              >查看</a>
              <a
                v-if="record.personLiable === $userInfo().id && record.salesAuditStatus === 'UNSOLD_ITEMS'"
                @click="handleEdit(record, 2) && hideBuquBtn()"
              >处理</a>
              <a
                v-if="isComment(record) && hideBuquBtn()"
                @click="handleEdit(record, 1, 1)"
              >批注 </a>
              <!-- 未销项，且法律风险状态为审核不通过、无风险时；责任部门所有人/合约法务部业务经理/合约法务部部门经理/区域法律顾问/总法律顾问有权限 -->
              <a
                  v-if="
                  (
                    record.salesAuditStatus === 'UNSOLD_ITEMS' &&
                    (record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED') &&
                    (record.orgId === $currentPost().deptId ||
                    $currentPost().postCode === 'BCBMLCMFLGW' ||
                    $currentPost().postCode === 'COLCMYWJL' ||
                    $currentPost().postCode === 'COLCMBMJL' ||
                    $currentPost().postCode === 'COLCMYWZB' ||
                    $currentPost().postCode === 'COLCMFLGW')
                  ) && hideBuquBtn()
                "
                @click="
                  chooseRow = Object.assign({}, { baseId: record.id }, record.riskLawBusVO || {})
                  visibleTwo = true
                "
              >法律风险</a>
              <!-- v-btn-permission="'lcm_006_fl_03'" -->
              <!-- v-btn-permission="'lcm_011_add_01'" -->
              <a
                v-if="isSale(record) && hideBuquBtn()"
                @click="
                  chooseRow = record
                  visible = true
                "
              >销项
              </a>
              <!-- <span v-if="record.auditStatus === 'DRAFT'" @click="handleDel(record.id)">删除 </span> -->
              <!-- 当状态为审批中 -->
              <a
                v-if="
                  record.auditStatus === 'PAST' ||
                    record.auditStatus === 'SUBMITTED' ||
                    record.auditStatus === 'REJECTED'
                "
                @click="
                  chooseRow = record
                  $refs.approval.open()
                "
              >审批详情</a>
            </MoreButton>
          </template>
        </list-table>
      </a-card>
      <!-- 销项弹窗 -->
      <sales
        :id="chooseRow.id"
        :visible.sync="visible"
        @ok="$refs.table.getData()"
      />
      <!-- 审批意见弹窗 -->
      <approval
        ref="approval"
        title="审批意见"
        :row="chooseRow"
      />
      <!-- 法律风险弹窗 -->
      <DialogForm
        ref="dialogForm"
        :visible.sync="visibleTwo"
        edit-model="E"
        :form="chooseRow"
        header-title="法律风险"
        :width="800"
        :rules="rules"
        :form-list="[{ type: 'BaseTextarea', keyName: 'content', label: '法律风险内容', span: 24 }]"
        @confirm="submitForm"
      />
     <selectApprover :businessId="businessId"  :processConfig="setProcessConfig" :isCloseUpper="false" @close="closeSelectApprover"></selectApprover>   
    </base-breadcrumb>
  </div>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectTreeDepartment from '@/components/Select/SelectTreeDepartment'
import SelectProject from '@/components/Select/SelectProject'
import OrgTreeSelect from '@/components/OrgTreeSelect'
import riskListApi from '@/api/legal/risk/list'
import sales from '@/views/Legal/risk/list/components/sales'
import StatusDisplay from '@/components/StatusDisplay'
import DialogForm from '@/components/DialogForm' // 弹窗
import approval from '@/views/Legal/risk/list/components/approval'

export default {
  name: 'RiskListIndex',
  components: {
    sales,
    StatusDisplay,
    approval,
    DialogForm
  },
  data() {
    return {
      visible: false, // 弹窗显隐控件
      visibleTwo: false, // 法律风险弹窗显隐控件
      chooseRow: {}, // 选中项
      statistics: {}, // 统计参数
      rules: {
        content: [{ required: true, whitespace: true, message: '请输入法律风险内容', trigger: 'change' }]
      }, // 校验规则
      statusMap: {
        DRAFT: {
          label: '无风险',
          color: '#999'
        },
        SUBMITTED: {
          label: '审核中',
          color: '#1890ff'
        },
        PAST: {
          label: '审核通过',
          color: '#00A854'
        },
        REJECTED: {
          label: '审批不通过',
          color: '#FE5500'
        }
      }, // 法律风险状态枚举
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          fixed: 'left',
          width: 70,
          scopedSlots: { customRender: 'index' }
        },
        {
          title: '所属分公司',
          ellipsis: true,
          dataIndex: 'branchCompany'
        },
        {
          title: '项目名称',
          ellipsis: true,
          dataIndex: 'project'
        },
        {
          title: '风险名称',
          ellipsis: true,
          dataIndex: 'riskName'
        },
        {
          title: '风险级别',
          dataIndex: 'riskLevelStr',
          width: 140
        },
        {
          title: '责任部门',
          ellipsis: true,
          dataIndex: 'orgName'
        },
        {
          title: '责任人',
          ellipsis: true,
          dataIndex: 'personLiableName'
        },
        {
          title: '处理时限',
          dataIndex: 'processingTime',
          width: 140
        },
        {
          title: '法律风险状态',
          dataIndex: 'auditStatusStr',
          scopedSlots: { customRender: 'auditStatusStr' },
          // fixed: 'right',
          width: this.STATUS_WIDTH
        },
        {
          title: '销项状态',
          dataIndex: 'salesAuditStatusStr',
          // fixed: 'right',
          width: this.STATUS_WIDTH
        },
        {
          title: '操作',
          width: 180,
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ], // 表头配置项
      customColumns: [
        {
          title: '序号',
          dataIndex: 'index'
        },
        {
          title: '所属分公司',
          dataIndex: 'branchCompany'
        },
        {
          title: '所属项目',
          dataIndex: 'project'
        },
        {
          title: '风险名称',
          dataIndex: 'riskName'
        },
        {
          title: '风险级别',
          dataIndex: 'riskLevelStr'
        },
        {
          title: '责任部门',
          dataIndex: 'orgName'
        },
        {
          title: '责任人',
          dataIndex: 'personLiableName'
        },
        {
          title: '完成时限',
          dataIndex: 'processingTime'
        },
        {
          title: '法律风险状态',
          dataIndex: 'auditStatusStr'
        },
        {
          title: '销项状态',
          dataIndex: 'salesAuditStatusStr'
        },
        {
          title: '创建人',
          dataIndex: 'creatorName'
        },
        {
          title: '创建时间',
          dataIndex: 'createTime'
        },
        {
          title: '销项时间',
          dataIndex: 'riskSalesBusVO.modifiedTime'
        },
        {
          title: '销项说明',
          dataIndex: 'riskSalesBusVO.salesContent'
        }
      ], // 导出表头
      searchArr: [
        // 搜索框
        {
          name: '所属单位',
          valueKey: 'searchQuery',
          type: OrgTreeSelect,
        },
        // {
        //   name: '所属分公司',
        //   valueKey: 'branchCompanyId',
        //   type: SelectSubCompany
        // },
        // {
        //   name: '项目名称',
        //   type: SelectProject,
        //   valueKey: 'projectId'
        // },
        {
          name: '风险名称',
          type: 'input',
          value: 'riskName'
        },
        {
          name: '风险级别',
          type: 'select',
          value: 'riskLevel',
          data: [
            { label: '一般风险', value: 1, enabled: true },
            { label: '较大风险', value: 2, enabled: true },
            { label: '重大风险', value: 3, enabled: true }
          ]
        },
        {
          name: '责任部门',
          valueKey: 'orgId',
          type: SelectTreeDepartment
        },
        {
          name: '责任人',
          type: 'input',
          value: 'personLiable'
        },
        // {
        //   name: '处理时限',
        //   type: 'dateRangeFormat',
        //   value: 'processingTime'
        // },
        {
          name: '法律风险状态',
          type: 'select',
          value: 'auditStatus',
          data: [
            { label: '无风险', value: 'DRAFT' },
            { label: '审核中', value: 'SUBMITTED' },
            { label: '审核通过', value: 'PAST' },
            { label: '审核不通过', value: 'REJECTED' }
          ]
        },
        {
          name: '销项状态',
          type: 'select',
          value: 'salesAuditStatus',
          data: [
            { label: '未销项', value: 'UNSOLD_ITEMS' },
            { label: '已销项', value: 'SOLD_ITEMS' }
          ]
        }
      ], // 搜索表单配置项
      loadData: async (params) => {
        let searchData = Object.assign(params, this.queryParam)
        riskListApi.findListCount(searchData).then((res) => {
          if (res.code === this.SUCCESS_CODE) this.statistics = res.data
        })
        return riskListApi.findListByMain(searchData)
      }, // 表格请求
      temp: () => {
        return (
          <span>
            无风险：{this.statistics.draft || 0}， 审核中：{this.statistics.submitted || 0}， 审核通过：
            {this.statistics.past || 0}， 审核不通过：{this.statistics.rejected || 0}， 未销项：
            {this.statistics.sealDraft || 0}, 已销项：
            {this.statistics.sealPast || 0}
          </span>
        )
      }, // 统计项vNode
      queryParam: {}, // 搜索参数集
      //获取流程配置值
      processConfig: null,
      //点击提交发送流程配置值
      setProcessConfig: null,
      businessId: null 
    }
  },
 async mounted(){
    //获取通过获取流程配置值
    this.processConfig = await this.$getConfigFn('legal_risk_law')  
  },
  methods: {
    /**
     * @description: 是否可标注
     * @param {*} record
     * @return {Boolean}
     */
    isComment(record) {
      // 未销项
      if (record.salesAuditStatus === 'UNSOLD_ITEMS') {
        // 法律风险状态为“审核通过”时
        if (record.auditStatus === 'PAST') {
          // 一般、较大风险：区域法律顾问可进行批注
          if ((record.riskLevel == 1 || record.riskLevel == 2) && this.$currentPost().postCode === 'BCBMLCMFLGW') {
            return true
          } else if (
            // 重大风险：合约法务部业务经理/合约法务部部门经理/总法律顾问可进行批注
            record.riskLevel == 3 &&
            (this.$currentPost().postCode === 'COLCMYWJL' ||
              this.$currentPost().postCode === 'COLCMBMJL' ||
              this.$currentPost().postCode === 'COLCMFLGW')
          ) {
            return true
          }
          return false
          // 法律风险状态不为“审核通过”时且为登陆人为当前责任部门人员
        } else if (record.orgId === this.$currentPost().deptId) {
          return true
        }
      }
      return false
    },
    /**
     * @description: 是否已销项
     * @param {*} record：列表操作项
     * @return {*}
     */
    isSale(record) {
      return (
        record.auditStatus !== 'SUBMITTED' &&
        record.salesAuditStatus === 'UNSOLD_ITEMS' &&
        record.personLiable &&
        record.personLiable === this.$userInfo()?.id
      )
    },
    /**
     * @description: 搜索
     * @param {*} val：搜索参数对象
     */
    search(val = {}) {
      this.queryParam = val
      this.$refs.table.getData(null, true)
    },
    /**
     *
     */
    /**
     * @description: 删除单行
     * @param {*} id：业务id
     */
    handleDel(id) {
      this.$delList2(
        riskListApi.deleteByRisk,
        () => {
          this.search()
        },
        { ids: [id] }
      )
    },
    /**
     * @description: 操作栏
     * @param {*} record：列表操作项
     * @param {*} step：1 编辑，2 处理
     * @param {*} isComment： 1 是否是批注
     */
    handleEdit(record = {}, step, isComment = 0) {
      this.$router.push({
        path: 'add',
        query: {
          id: (record && record.id) || '',
          step,
          isComment
        }
      })
    },
    /**
     * @description: 法律风险提交表单
     * @param {*} form:表单
     */
    submitForm(form) {
      let that = this
      that.setProcessConfig = null;   
      const name = form.id ? 'editByRiskLaw' : 'createByRiskLaw' // 新增或编辑

       that.submitApprovalConfirm(this.processConfig, async (flag)=>{
          if(!flag) {
            this.loading = false
            return
          }
          return riskListApi[name](form).then((res) => {
            if (res.code === 200) {
              that.$noticeSuccess('提交成功')
              //提交时，返回id
              that.businessId = res.data.businessId;
              that.setProcessConfig = that.processConfig;
              that.$refs.table.getData()
              that.visibleTwo = false
            }
          })
          })
    },
    closeSelectApprover(){
      this.visibleTwo = false;
      this.$router.push({ path: '/legal/risk/list/index' })
    }
  }
}
</script>
